<template>
	<div class="system-menu-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" draggable :close-on-click-modal="false" width="769px">
			<el-form ref="menuDialogFormRef" :model="state.menuForm" size="default" label-width="80px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="上级菜单">
							<el-cascader
								:options="props.menuData"
								:props="{ checkStrictly: true, value: 'id', label: 'title', emitPath: false }"
								placeholder="请选择上级菜单"
								clearable
								class="w100"
								v-model="state.menuForm.pid"
							>
								<template #default="{ node, data }">
									<span>{{ data.title }}</span>
									<span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
								</template>
							</el-cascader>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="菜单类型" prop="type" :rules="[{ required: true, message: '菜单类型不能为空', trigger: 'blur' }]">
							<el-radio-group v-model="state.menuForm.type">
								<el-radio :value="1">目录</el-radio>
								<el-radio :value="2">菜单</el-radio>
								<el-radio :value="3">按钮</el-radio>
							</el-radio-group>
						</el-form-item>


						<!-- <el-form-item label="菜单类型">
							<el-radio-group v-model="state.menuForm.type" :rules="[{ required: true, message: '菜单类型不能为空', trigger: 'blur' }]">
								<el-radio :label="0">目录</el-radio>
								<el-radio :label="1">菜单</el-radio>
								<el-radio :label="2">按钮</el-radio>
							</el-radio-group>
						</el-form-item> -->
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="菜单名称" prop="title" :rules="[{ required: true, message: '菜单名称不能为空', trigger: 'blur' }]">
							<el-input v-model="state.menuForm.title" placeholder="路由中的 Title 值" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="路由名称">
							<el-input v-model="state.menuForm.name" placeholder="路由中的 name 值" clearable></el-input>
						</el-form-item>
					</el-col>
					<template v-if="state.menuForm.type === 1 || state.menuForm.type === 2">
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="路由路径">
								<el-input v-model="state.menuForm.path" placeholder="路由中的 path 值" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="重定向">
								<el-input v-model="state.menuForm.redirect" placeholder="请输入路由重定向" clearable></el-input>
							</el-form-item>
						</el-col>
					</template>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="菜单图标">
								<IconSelector placeholder="请输入菜单图标" v-model="state.menuForm.icon" type="all" />
							</el-form-item>
						</el-col>
					<template v-if="state.menuForm.type === 1 || state.menuForm.type === 2">
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="组件路径">
								<el-input v-model="state.menuForm.component" placeholder="组件路径" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="链接地址">
								<el-input
									v-model="state.menuForm.url"
									placeholder="外链/内嵌时链接地址（http:xxx.com）"
									clearable
								>
								</el-input>
							</el-form-item>
						</el-col>
					</template>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="权限标识"  :rules="[{ required: true, message: '权限标识不能为空', trigger: 'blur' }]">
							<el-input v-model="state.menuForm.permission" placeholder="请输入权限标识" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="菜单排序">
							<el-input-number v-model="state.menuForm.orderNo" controls-position="right" placeholder="请输入排序" class="w100" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="是否启用">
							<el-radio-group v-model="state.menuForm.status">
								<el-radio :value="0">启用</el-radio>
								<el-radio :value="1">不启用</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
					<template v-if="state.menuForm.type !== 3">
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="是否隐藏">
								<el-radio-group v-model="state.menuForm.isHide">
									<el-radio :value="true">隐藏</el-radio>
									<el-radio :value="false">不隐藏</el-radio>
								</el-radio-group>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="页面缓存">
								<el-radio-group v-model="state.menuForm.isKeepAlive">
									<el-radio :value="true">缓存</el-radio>
									<el-radio :value="false">不缓存</el-radio>
								</el-radio-group>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="是否固定">
								<el-radio-group v-model="state.menuForm.isAffix">
									<el-radio :value="true">固定</el-radio>
									<el-radio :value="false">不固定</el-radio>
								</el-radio-group>
							</el-form-item>
						</el-col>
						<!-- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="是否外链">
								<el-radio-group v-model="state.ruleForm.isLink" :disabled="state.ruleForm.meta.isIframe">
									<el-radio :label="true">是</el-radio>
									<el-radio :label="false">否</el-radio>
								</el-radio-group>
							</el-form-item>
						</el-col> -->
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
							<el-form-item label="是否内嵌">
								<el-radio-group v-model="state.menuForm.isIframe">
									<el-radio :value="true">是</el-radio>
									<el-radio :value="false">否</el-radio>
								</el-radio-group>
							</el-form-item>
						</el-col>
					</template>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="备注">
							<el-input v-model="state.menuForm.remarks" placeholder="请输入备注内容" clearable type="textarea" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemMenuDialog">
import { defineAsyncComponent, reactive,  ref } from 'vue';
import { useMenuApi } from '/@/api/menu';
import { ElMessage } from 'element-plus';
const props = defineProps({
	title: String,
	menuData: Array<MenuItem>,
});
// 定义子组件向父组件传值/事件
const emit = defineEmits(['getTableData']);

// 引入组件
const IconSelector = defineAsyncComponent(() => import('/@/components/iconSelector/index.vue'));

// 定义变量内容
const menuDialogFormRef = ref();
//const stores = useRoutesList();
//const { routesList } = storeToRefs(stores);
const state = reactive({
	menuForm: {} as MenuForm,
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 获取 pinia 中的路由
// const getMenuData = (routes: RouteItems) => {
// 	const arr: RouteItems = [];
// 	routes.map((val: RouteItem) => {
// 		val['title'] = i18n.global.t(val.meta?.title as string);
// 		arr.push({ ...val });
// 		if (val.children) getMenuData(val.children);
// 	});
// 	return arr;
// };
// 打开弹窗
const openDialog = (type: string, row?: MenuForm) => {
	//console.log('type:'+type+',row:'+JSON.stringify(row));
	if (type === 'edit') {
		// 模拟数据，实际请走接口
		// row.menuType = 'menu';
		// row.menuSort = Math.floor(Math.random() * 100);
		state.menuForm = row; //JSON.parse(JSON.stringify(row));
		state.dialog.title = '修改菜单';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增菜单';
		state.dialog.submitTxt = '新 增';
		state.menuForm = row;
		// 清空表单，此项需加表单验证才能使用
		// nextTick(() => {
		// 	menuDialogFormRef.value.resetFields();
		// });
	}
	state.dialog.type = type;
	state.dialog.isShowDialog = true;
	//console.log('menuForm:'+JSON.stringify(state.menuForm));
	//console.log('type:'+state.menuForm.type);
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 是否内嵌下拉改变
// const onSelectIframeChange = () => {
// 	if (state.menuForm.isIframe) state.menuForm.url = true;
// 	else state.ruleForm.isLink = false;
// };
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	menuDialogFormRef.value.validate(async (valid: boolean) => {
		if (!valid) return;
		if (state.menuForm.id != undefined && state.menuForm.id > 0) {
			useMenuApi().editAdminMenu(state.menuForm).then(async (res: AjaxResult) => {
				if (res.success == true) {
					ElMessage.success('更新成功');
					emit('getTableData');
					closeDialog();
				}
				else {
					ElMessage.warning('更新失败：' + res.message);
				}
			});
		} else {
			useMenuApi().addAdminMenu(state.menuForm).then(async (res: AjaxResult) => {
				if (res.success == true) {
					ElMessage.success('新增成功');
					emit('getTableData');
					closeDialog();
				}
				else {
					ElMessage.warning('新增失败：' + res.message);
				}
			});
		}
	});
	// state.tableData.loading = true;
	// const _res : AjaxResult = useMenuApi().getAdminMenu(
	// closeDialog(); // 关闭弹窗
	// emit('refresh');
	// if (state.dialog.type === 'add') { }
	// setBackEndControlRefreshRoutes() // 刷新菜单，未进行后端接口测试
};
// 页面加载时
// onMounted(() => {
// 	state.menuData = [];// getMenuData(routesList.value);
// });

// 暴露变量
defineExpose({
	openDialog,
});
</script>
